package com.ruoyi.cc.controller;

import com.ruoyi.cc.DTO.QueueConsumeDTO;
import com.ruoyi.cc.DTO.QueueCreateDTO;
import com.ruoyi.cc.DTO.Result;
import com.ruoyi.cc.domain.*;
import com.ruoyi.cc.service.IMerchantService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

import static com.ruoyi.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("/merchant")
public class MerchantController {
    @Autowired
    private IMerchantService merchantService;
     /**
      * 查询商户列表
      *
      * @return 商户列表
      */
    @RequestMapping("/list")
    public Map<String, Object> list(Merchant merchant) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectMerchantList(merchant));
        return result;
    }
     /**
      * 查询商户图片列表
      *
      * @return 商户图片列表
      */
    @RequestMapping("/imgList")
    public Map<String, Object> imgList(MerchantImgs merchantImgs) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectMerchantImgList(merchantImgs));
        return result;
    }
     /**
      * 根据店铺id查询服务详情列表
      *
      * @return 服务详情列表
      */
    @RequestMapping("/serviceDetailsList")
    public Map<String, Object> serviceDetailsList(ServiceDetails serviceDetails) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectServiceDetailsListByMerchantId(serviceDetails));
        return result;
    }
     /**
      * 根据店铺id查询评价列表
      *
      * @return 评价列表
      */
    @RequestMapping("/evaluateList")
    public Map<String, Object>  evaluateList(evaluate evaluate) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectEvaluateListByMerchantId(evaluate));
        return result;
    }
     /**
      * 查询评价数量
      *
      * @return 评价数量
      */
    @RequestMapping("/evaluateCount")
    public Map<String, Object> evaluateCount(evaluate evaluate) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectEvaluateCountByMerchantIdAndUserId(evaluate));
        return result;
    }
     /**
      * 提交维修预约的订单
      */
    @RequestMapping("/repairReservation")
    public Map<String, Object> repairReservation(repairReservation repairReservation) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.insertRepairReservation(repairReservation));
        return result;
    }
     /**
      * 查询车辆类型列表
      *
      * @return 车辆类型列表
      */
    @RequestMapping("/carTypeList")
    public Map<String, Object> carTypeList() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectCarTypeList());
        return result;
    }
     /**
      * 添加订单
      *
      * @return 订单列表
      */
    @RequestMapping("/insertOrder")
    public Map<String, Object> insertOrder(Order order) {
        String uuid = UUID.randomUUID().toString();
        Map<String, Object> result = new HashMap<>();
        order.setVerificationCode(uuid);
        order.setOrderTime(new Date());
        order.setServiceTypeId(2);
        order.setPayStatus(0);
        order.setStatus(0);
        order.setDeleted(0);
        order.setEvaluate(0);
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.insertOrder(order));
        return result;
    }

    /**
     * 创建队列接口（POST请求）
     * @param dto 前端传入的请求参数（已通过@Validated校验）
     * @return 接口响应信息
     */
    @PostMapping("/create")
    public Result<String> createQueue(@Validated @RequestBody QueueCreateDTO dto) {
        try
        {   merchantService.createQueue(dto);
            String message = "队列创建成功！队列名：" + dto.getName() + "，延迟删除时间：" + dto.getLastTime() + "秒，消息数量：" + dto.getNumber();
            return Result.success(message);
        }
        catch (Exception e)
        {
            // 显式指定泛型为String，使返回类型匹配Result<String>
            return Result.fail(500, e.getMessage());
        }
    }
    /**
     * 消费指定队列的单条消息接口
     * @PreAuthorize("hasPermi('rabbit:queue:consume')") // 可选：若依细粒度权限控制（需在后台配置权限）
     * @PostMapping：HTTP请求方式为POST（适合传递JSON参数）
     * @RequestMapping("/consume")：接口子路径，完整路径为/rabbit/queue/consume
     * @Validated：启用参数校验（触发QueueConsumeDTO中的@NotBlank校验）
     * @RequestBody：接收JSON格式入参，自动映射到QueueConsumeDTO对象
     * @param dto 消费消息请求参数（包含队列名称name）
     * @return AjaxResult：若依统一响应格式（code=200成功，code=500失败）
     */
    @PostMapping("/consume")
    public Result<List<String>> consumeMessage(@Validated @RequestBody QueueConsumeDTO dto) {
        // 调用Service层消费消息方法，获取消息内容
        String messageContent = merchantService.consumeMessage(dto);
        // 若依success()方法：返回统一成功响应，data字段携带消息内容
        if(messageContent == null)
        {
            return Result.fail(500, "优惠券已领取完");
        }else {
            return Result.success(Collections.singletonList(messageContent));
        }

    }
     /**
      * 改变优惠券状态
      */
    @RequestMapping("/updateCouponStatus")
    public Map<String, Object> updateCouponStatus(String queueName) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.updateCouponStatus(queueName));
        return result;
    }
     /**
      * 增加优惠券
      */
    @RequestMapping("/insertCoupon")
    public Map<String, Object> insertCoupon(Coupon coupon) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.insertCoupon(coupon));
        return result;
    }
     /**
      * 查询所有优惠券列表
      *
      * @return 优惠券列表
      */
    @RequestMapping("/selectAllCouponList")
    public Map<String, Object> selectAllCouponList() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectAllCouponList());
        return result;
    }
     /**
      * 查询每个用户自己未领取的优惠券列表
      *
      * @return 优惠券列表
      */
    @RequestMapping("/selectCouponList")
    public Map<String, Object> selectCouponList(Coupon coupon) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectCouponList(coupon));
        return result;
    }
     /**
      * 查询每个用户自己已领取过的且可再次领取的优惠券列表
      *
      * @return 优惠券列表
      */
    @RequestMapping("/selectCouponListAgain")
    public Map<String, Object> selectCouponListAgain(Coupon coupon) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.selectCouponListAgain(coupon));
        return result;
    }
     /**
      * 领取优惠券成功后在用户表中增加该优惠券
      */
    @RequestMapping("/insertCouponToUser")
    public Map<String, Object> insertCouponToUser(userCoupon userCoupon) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.insertCouponToUser(userCoupon));
        return result;
    }
     /**
      * 领取已领取过的但可再次领取的优惠券成功后在用户优惠券表中增加该优惠券的数量
      */
    @RequestMapping("/updateUserCouponStatus")
    public Map<String, Object> updateUserCouponStatus(userCoupon userCoupon) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "success");
        result.put("data", merchantService.updateUserCouponStatus(userCoupon));
        return result;
    }
}
